'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 设置进攻阵容
 * @author agl
 */

var _ = require('lodash');
var async = require('async');
var moment = require('moment');
var Code = require('../../../../consts/code');
var moduleType = require('../../../../consts/moduleType');
var operationType = require('../../../../consts/operationType');
var configUtil = require('../../../../util/configUtil');
var constantUtil = require('../../../../util/constantUtil');
var PlayerManager = require('../../../../libs/playerManager');
var taskService = require('../../../../services/taskService');
var roleService = require('../../../../services/roleService');
var soldierService = require('../../../../services/soldierService.js');
var arenaService = require('../../../../services/arenaService.js');

/**
 * 设置竞技场阵容
 */
module.exports = function (msg, session, next) {
    var role_id = session.uid;
    var opt_type = msg.opt_type;
    var soldier_type_list = msg.soldier_type_list;

    if (!opt_type || !soldier_type_list) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(['soldier', 'arena', 'accrued'], cb);
            },
            // jshint maxstatements:100
            function (cb) {
                var soldier_model = player_manager.soldier;
                var arena_model = player_manager.arena;

                // 判断已经选择的兵种个数是否大于最大值
                var max_soldier_type_count = constantUtil.value('wall_soldier_type_count');
                if (soldier_type_list.length > max_soldier_type_count) {
                    soldier_type_list = _.slice(soldier_type_list, 0, max_soldier_type_count);
                }

                // 判断兵种是否已经拥有
                var total_cost_space = 0;
                for (var i = soldier_type_list.length - 1; i >= 0; i--) {
                    var soldier_type = soldier_type_list[i];

                    if (soldier_type > 0) {
                        var soldier_item = soldier_model.getSoldier(soldier_type);
                        if (!soldier_item) {
                            return next(null, {
                                code: Code.SOLDIER_NOT_EXIST
                            });
                        }
                    }
                }

                // 保存选择的兵种
                arena_model.setSoldierList(opt_type, soldier_type_list);

                // 计算战力
                arenaService.uupdateArenaPower(player_manager, opt_type);

                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK
            });
        }
    );
};
